package com.mes.common.util;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CodeUtil {

	@Autowired
	SysUtil sysUtil;
	
	private static final Log logger = LogFactory.getLog(CodeUtil.class);
	
	public String getCodeValue(String codeName, Map<String, Object> featuresMap, String creatorUid) throws Exception {
		Document document = new Document();
		Element root = new Element("data");
		document.addContent(root);
		
		Element ele = new Element("codeName");
		ele.setText(codeName);
		root.addContent(ele);
		
		ele = new Element("creatorUid");
		ele.setText(creatorUid);
		root.addContent(ele);
		
		Element features = new Element("features");
		root.addContent(features);	
		
		for (String s : featuresMap.keySet()) {
			ele = new Element(s);
			ele.setText((String)featuresMap.get(s));
			features.addContent(ele);
		}
		
		String xml = doc2String(document);
//		logger.error("准备向编码服务系统发送的数据是：--->" + xml);
		
		String wsdlUrl = sysUtil.getProperty("codeWebServiceURL");
		// 直接引用远程的wsdl文件
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(wsdlUrl);
		call.setOperationName("getCodeValueByXml");// WSDL里面描述的接口名称
		call.addParameter("dataXML",
				org.apache.axis.encoding.XMLType.XSD_STRING,
				javax.xml.rpc.ParameterMode.IN);// 接口的参数
		call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型
		call.setTimeout(5000);
		// 给方法传递参数，并且调用方法
		String code = (String) call.invoke(new Object[] { xml });
		logger.error("编码服务系统返回结果是：--->" + code);
		
		return code;
	}
	
	/**
	 * Document转换为字符串
	 * 
	 * @param xmlFilePath
	 *            XML文件路径
	 * @return xmlStr 字符串
	 * @throws Exception
	 */
	public static String doc2String(Document doc) throws Exception {
		Format format = Format.getPrettyFormat();
		format.setEncoding("GBK");// 设置xml文件的字符为UTF-8，解决中文问题
		XMLOutputter xmlout = new XMLOutputter(format);
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		xmlout.output(doc, bo);
		return bo.toString();
	}
	
	/**
	 * 通过编码服务获取废品单单号编码
	 * @return
	 * @throws Exception
	 */
	public String getRejectBillNumber() throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		// TODO 此处需要根据废品单的编码定义来组织featuresMap
		
		billNumber = this.getCodeValue("bfd", featuresMap, SysUtil.getSessionEmployeeUid());
		return billNumber;
	}
	
	/**
	 * 通过编码服务获取合格证单号编码
	 * @return
	 * @throws Exception
	 */
	public String getCertificateBillNumber(String deptShortName) throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		
		// TODO 此处需要根据合格证的编码定义来组织featuresMap
		featuresMap.put("bm", deptShortName);
		
		billNumber = this.getCodeValue("hgz", featuresMap, SysUtil.getSessionEmployeeUid());
		return billNumber;
	}
	
	/**
	 * 通过编码服务获取返修合格证单号编码
	 * @param oldCertificateId 原合格证号
	 * @return
	 * @throws Exception
	 */
	public String getFXCertificateBillNumber(String oldCertificateId) throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		
		// TODO 此处需要根据返修合格证的编码定义来组织featuresMap
		featuresMap.put("certificateId", oldCertificateId);
		
		billNumber = this.getCodeValue("fxhgz", featuresMap, SysUtil.getSessionEmployeeUid());
		return billNumber;
	}
	
	
	public void testCode() throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		// TODO 此处需要根据废品单的编码定义来组织featuresMap
		featuresMap.put("bm", "Dbc");
		
		billNumber = this.getCodeValue("hgz", featuresMap, SysUtil.getSessionEmployeeUid());
		
	}
	
	/**
	 * 通过编码服务获取出库单号编码
	 * @return
	 * @throws Exception
	 */
	public String getInvOutBillNumber() throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		
		// TODO 此处需要根据返修合格证的编码定义来组织featuresMap
		
		billNumber = SysUtil.getUUID();
		return billNumber;
	}
	
	/**
	 * 通过编码服务获取入库单号编码
	 * @return
	 * @throws Exception
	 */
	public static String getInvInBillNumber() throws Exception {
		String billNumber = "";
		Map<String, Object> featuresMap = new HashMap<String, Object>();
		
		// TODO 此处需要根据返修合格证的编码定义来组织featuresMap
		
		billNumber = SysUtil.getUUID();
		return billNumber;
	}
	
}
